﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using CMS.BLL;

public partial class Controls_HomePage_ctrRecentComment : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            this.InitData(); 
        }
    }

    private void InitData()
    { 
            int nCurrentLanguage = Support.GetCurrentLanguage();
            int nPageCount = 0;

            lblRecentComment.Text = MultipleLanguage.RECENT_COMMENT[nCurrentLanguage -1];

            NewsComment objNewsComment = new NewsComment();
            DataTable dtbNewsComment = new DataTable();
            dtbNewsComment = objNewsComment.Search(0, "", 0, 1, 1, 3, ref nPageCount);
            rptComment.DataSource = dtbNewsComment;
            rptComment.DataBind();
    }
    protected void rptComment_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        int nCurrentLanguage = Support.GetCurrentLanguage(); 
        if ((e.Item.ItemType != ListItemType.Header) && (e.Item.ItemType != ListItemType.Footer))
        { 
        Label lblComment = (Label)e.Item.FindControl("lblComment");
        Label lblUserID = (Label)e.Item.FindControl("lblUserID");

        int nID = ConvertData.ConvertToInt(DataBinder.Eval(e.Item.DataItem, "CommentID"));
        int nNewsID = ConvertData.ConvertToInt(DataBinder.Eval(e.Item.DataItem, "NewsID"));

        string sComment = ConvertData.ConvertToString(DataBinder.Eval(e.Item.DataItem, "Content"));
        int nPostedDate = ConvertData.ConvertToInt(DataBinder.Eval(e.Item.DataItem, "Dateposted"));
        int nUserID = ConvertData.ConvertToInt(DataBinder.Eval(e.Item.DataItem, "UserID"));
        lblUserID.Text = ConvertData.ConvertIntToDatetimeString(nPostedDate ) + "," + " " + MultipleLanguage.BY[nCurrentLanguage - 1] + " " + this.GetUserNameByUserID(nUserID) ;
        if (sComment.Length >55)
        { sComment = ConvertData.TruncateString(sComment, 55) + "..."; }
        lblComment.Text = sComment;
        }
    }

    private string GetUserNameByUserID(int nUserID)
    {
        string sUserName = string.Empty;
        Users objUser = new Users();
        objUser.LoadById(nUserID);
        sUserName = objUser.Data.Username;
        return sUserName;
    }
}
